import { Request, Response, NextFunction } from 'express';

// 扩展 Response 接口
declare global {
    namespace Express {
        interface Response {
            success: (data?: any, message?: string) => void;
            error: (message: string, statusCode?: number) => void;
        }
    }
}

export const responseFormatter = (req: Request, res: Response, next: NextFunction) => {
    // 成功响应方法
    res.success = (data?: any, message: string = '操作成功') => {
        res.json({
            status: 'success',
            message,
            data
        });
    };

    // 错误响应方法
    res.error = (message: string, statusCode: number = 400) => {
        res.status(statusCode).json({
            status: 'error',
            message
        });
    };

    next();
}; 